
const animation = weex.requireModule('animation');
var keys = {}; //动画关键字
export default {

  /** 开始动画
   * 
   * @param {*} v 旋转对象 
   * @param {Number} time 旋转时间毫秒 
   * @param {Number} angle 旋转角度 
   * @param {Number} num 旋转圈数 -1无限旋转
   */
  start(key, v, time, angle, num){
    if (key == undefined || v == undefined) return;

    var i = keys[key];
    i = (i == undefined) ? 1 : (i + 1)
    keys[key] = i;
    
    var param = this.getParam((angle * i), time);
    animation.transition(v, param, ()=>{
      if (keys[key] == undefined) return;

      num = num > 0 ? (num - 1) : num;
      if (num == 0) {
        this.end(key, v);
      } else {
        this.start(key, v, time, angle, num);
      }
    });
  },

  //动画结束
  end(key, v) {
    if (key == undefined || v == undefined) return;
    delete keys[key];
    var param = this.getParam(0, 0);
    animation.transition(v, param);
  },
  
  /** 获取动画参数
   * 
   * @param {Number} row 1=180度
   * @param {Number} time 旋转时间
   * @returns 
   */
  getParam(angle, time) {
    var param = {
      styles: {
        transform: 'rotate('+angle+'deg)',
      },
      duration: time, //ms
      timingFunction: 'linear',
      needLayout:false,
      delay: 0 //ms
    };
    return param;
  },
}
